home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp® 2.0.1 Tutorial / Chapter 09 / UIconEdit.inc1.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  8.4 KB  |  313 lines  |  [TEXT/MPS ]

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.     
  14.     { Resource identifiers }
  15.     
  16.     kSeedIconId =        1000;                        { Id of the seed icon resource.            }
  17.  
  18.     kIconWindowId =        1000;                        { Id of the icon window resource.        }
  19.  
  20.  
  21. TYPE
  22.     LongArrayHdl =        ^LongArrayPtr;
  23.     LongArrayPtr =        ^LongArray;
  24.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  25.  
  26.  
  27.  
  28. {-------------------------------------------------------------------------------------------}
  29. {--------------------------------TIconApplication methods-----------------------------------}
  30. {-------------------------------------------------------------------------------------------}
  31.  
  32. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  33.  
  34. BEGIN
  35.     IApplication(iconFileType);
  36. END;
  37.  
  38.  
  39.  
  40. {-------------------------------------------------------------------------------------------}
  41.  
  42. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  43.  
  44. VAR
  45.     anIconDocument:        TIconDocument;
  46.  
  47. BEGIN
  48.     New(anIconDocument);                            { Create a TIconDocument object.        }
  49.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  50.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  51.  
  52.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  53. END;
  54.     
  55.     
  56.     
  57.     
  58. {-------------------------------------------------------------------------------------------}
  59. {----------------------------------TIconDocument methods------------------------------------}
  60. {-------------------------------------------------------------------------------------------}
  61.  
  62. PROCEDURE TIconDocument.IIconDocument;
  63.  
  64. VAR anIconBitMap : TIconBitMap;
  65.  
  66. BEGIN
  67.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  68.                                                     { fails, TIconDocument.Free works okay.    }
  69.  
  70.     IDocument(kFileType,                             { This document's file type.            }
  71.               kSignature,                             { This document's creator.                }
  72.               kUsesDataFork,                         { This document does use the data fork    }
  73.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  74.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  75.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  76.  
  77.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  78.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  79.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  80.     
  81.     fIconBitMap := anIconBitMap;                    { Store a reference to it in a field.   }
  82. END;
  83.  
  84.  
  85.  
  86. {-------------------------------------------------------------------------------------------}
  87.  
  88. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  89.     { This method is called to set the document's data to the "new" state, as when the user    }
  90.     { chooses to open a new document instead of an existing one.  We set the value of the     }
  91.     { document's icon bit map to that of a "seed" icon in our resource file.  That way we     }
  92.     { can the document's initial state simply by changing the "seed" icon resource.            }
  93.  
  94. VAR
  95.     seedIcon:        Handle;
  96.  
  97. BEGIN
  98.     seedIcon := GetIcon(kSeedIconId);                { Get the seed icon resource.            }
  99.     FailNilResource(seedIcon);
  100.     fIconBitMap.SetIconBitMap(seedIcon)
  101. END;
  102.  
  103.  
  104.  
  105. {-------------------------------------------------------------------------------------------}
  106.  
  107. PROCEDURE TIconDocument.Free; OVERRIDE;
  108.     
  109. BEGIN
  110.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  111.  
  112.     INHERITED Free;
  113. END;
  114.  
  115.  
  116.  
  117. {-------------------------------------------------------------------------------------------}
  118.  
  119. PROCEDURE TIconDocument.DoMakeViews(forPrinting: boolean); OVERRIDE;
  120.  
  121. VAR
  122.     aWindow:            TWindow;
  123.  
  124. BEGIN
  125.     IF NOT forPrinting THEN
  126.         aWindow := NewTemplateWindow(kIconWindowId, SELF);        { Create the window. }
  127. END;
  128.  
  129.         
  130.  
  131. {-------------------------------------------------------------------------------------------}
  132.  
  133. PROCEDURE TIconDocument.Fields (PROCEDURE DoToField (fieldName: Str255;
  134.                                                    fieldAddr: Ptr;
  135.                                                    fieldType: INTEGER)); OVERRIDE;
  136.  
  137. BEGIN
  138.     DoToField('TIconDocument', NIL, bClass);
  139.     DoToField('fIconBitMap', @fIconBitMap, bObject);
  140.  
  141.     INHERITED Fields(DoToField);
  142. END;
  143.  
  144.     
  145. {-------------------------------------------------------------------------------------------}
  146. {-----------------------------------TIconBitMap methods-------------------------------------}
  147. {-------------------------------------------------------------------------------------------}
  148.  
  149. PROCEDURE TIconBitMap.IIconBitMap;
  150.  
  151. BEGIN
  152.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  153.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  154. END;
  155.  
  156.  
  157.  
  158. {-------------------------------------------------------------------------------------------}
  159.  
  160. PROCEDURE TIconBitMap.Free; OVERRIDE;
  161.  
  162. BEGIN
  163.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  164. END;
  165.  
  166.  
  167.  
  168. {-------------------------------------------------------------------------------------------}
  169.  
  170. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  171.  
  172. BEGIN
  173.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  174.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  175. END;
  176.             
  177.             
  178.  
  179. {-------------------------------------------------------------------------------------------}
  180.  
  181. PROCEDURE TIconBitMap.Clear;
  182.  
  183. VAR
  184.     iconAsLongArray:    LongArrayHdl;
  185.     i:                    INTEGER;
  186.  
  187. BEGIN
  188.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  189.  
  190.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  191.         iconAsLongArray^^[i] := 0;
  192. END;
  193.  
  194.  
  195.  
  196. {-------------------------------------------------------------------------------------------}
  197.  
  198. PROCEDURE TIconBitMap.Invert;
  199.  
  200. VAR
  201.     iconAsLongArray:    LongArrayHdl;
  202.     i:                    INTEGER;
  203.  
  204. BEGIN
  205.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  206.  
  207.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  208.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  209. END;
  210.  
  211.  
  212.  
  213. {-------------------------------------------------------------------------------------------}
  214.  
  215. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  216.     { This converts the given icon bit to a word and bit in an array of long words. }
  217.  
  218. VAR
  219.     bitNumber:        INTEGER;
  220.  
  221. BEGIN
  222.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  223.     word := bitNumber DIV 32;
  224.     bit := 31 - (bitNumber MOD 32);
  225. END;
  226.  
  227.  
  228.  
  229. {-------------------------------------------------------------------------------------------}
  230.  
  231. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  232.     { Returns the state of the given bit in the icon being drawn. }
  233.  
  234. VAR
  235.     word:            INTEGER;
  236.     bitInWord:        INTEGER;
  237.  
  238. BEGIN
  239.     IconBitToWordBit(iconBit, word, bitInWord);
  240.  
  241.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  242. END;
  243.  
  244.  
  245.  
  246. {-------------------------------------------------------------------------------------------}
  247.  
  248. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  249.     { Set the state of the given bit in the icon being drawn. }
  250.  
  251. VAR
  252.     word:            INTEGER;
  253.     bitInWord:        INTEGER;
  254.  
  255. BEGIN
  256.     IconBitToWordBit(iconBit, word, bitInWord);
  257.  
  258.     {$H-}                                            { So the compiler thinks this is unsafe.}
  259.     IF turnBitOn THEN
  260.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  261.     ELSE
  262.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  263.     {$H+}
  264. END;
  265.  
  266.  
  267.  
  268. {-------------------------------------------------------------------------------------------}
  269.  
  270. FUNCTION TIconBitMap.Copy: TIconBitMap;
  271.  
  272. VAR
  273.     copyOfIcon:        TIconBitMap;
  274.  
  275. BEGIN
  276.     New(copyOfIcon);                                { Create a TIcon object.                }
  277.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  278.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  279.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  280.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  281. END;
  282.  
  283.  
  284.             
  285. {-------------------------------------------------------------------------------------------}
  286.  
  287. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  288.  
  289. BEGIN
  290.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  291. END;
  292.  
  293.  
  294. {-------------------------------------------------------------------------------------------}
  295.  
  296. PROCEDURE TIconBitMap.Fields (PROCEDURE DoToField (fieldName: Str255;
  297.                                                    fieldAddr: Ptr;
  298.                                                    fieldType: INTEGER)); OVERRIDE;
  299.  
  300. BEGIN
  301.     DoToField('TIconBitMap', NIL, bClass);
  302.     DoToField('fDataHandle', @fDataHandle, bHandle);
  303.  
  304.     INHERITED Fields(DoToField);
  305. END;
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.